home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1L8ON9C (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  2.6 KB  |  68 lines

  1. package com.sun.java.swing.table;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.JLabel;
  5. import com.sun.java.swing.JTable;
  6. import com.sun.java.swing.UIManager;
  7. import com.sun.java.swing.border.Border;
  8. import com.sun.java.swing.border.EmptyBorder;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.io.Serializable;
  12.  
  13. public class DefaultTableCellRenderer extends JLabel implements TableCellRenderer, Serializable {
  14.    protected static Border noFocusBorder;
  15.    private Color unselectedForeground;
  16.    private Color unselectedBackground;
  17.  
  18.    public DefaultTableCellRenderer() {
  19.       noFocusBorder = new EmptyBorder(1, 2, 1, 2);
  20.       ((JComponent)this).setOpaque(true);
  21.       ((JComponent)this).setBorder(noFocusBorder);
  22.    }
  23.  
  24.    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  25.       if (isSelected) {
  26.          super.setForeground(table.getSelectionForeground());
  27.          super.setBackground(table.getSelectionBackground());
  28.       } else {
  29.          super.setForeground(this.unselectedForeground != null ? this.unselectedForeground : ((Component)table).getForeground());
  30.          super.setBackground(this.unselectedBackground != null ? this.unselectedBackground : ((Component)table).getBackground());
  31.       }
  32.  
  33.       ((JLabel)this).setFont(((Component)table).getFont());
  34.       if (hasFocus) {
  35.          ((JComponent)this).setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
  36.          if (table.isCellEditable(row, column)) {
  37.             super.setForeground(UIManager.getColor("Table.focusCellForeground"));
  38.             super.setBackground(UIManager.getColor("Table.focusCellBackground"));
  39.          }
  40.       } else {
  41.          ((JComponent)this).setBorder(noFocusBorder);
  42.       }
  43.  
  44.       this.setValue(value);
  45.       return this;
  46.    }
  47.  
  48.    public void setBackground(Color c) {
  49.       super.setBackground(c);
  50.       this.unselectedBackground = c;
  51.    }
  52.  
  53.    public void setForeground(Color c) {
  54.       super.setForeground(c);
  55.       this.unselectedForeground = c;
  56.    }
  57.  
  58.    protected void setValue(Object value) {
  59.       ((JLabel)this).setText(value == null ? "" : value.toString());
  60.    }
  61.  
  62.    public void updateUI() {
  63.       super.updateUI();
  64.       this.setForeground((Color)null);
  65.       this.setBackground((Color)null);
  66.    }
  67. }
  68.